
package JET.SA;

import JET.Centers.ControlCenter;
import JET.Frames.DataModels.DataModelsFactory;
import JET.Scene;
import JET.SceneHelper;
import JET.StaticActions;
import JET.physics.Mass;
import JET.physics.bodies.SpringBody;
import JET.physics.steering.Torque;
import java.util.ArrayList;
import java.util.Collection;

/**
 *
 * @author 057Ry
 */
public class SA_Mass {

    private static ControlCenter CC = ControlCenter.getInstance();

    ////////////////////////////////////////////////////////////////////////////
    //                             M A S S                                    //
    ////////////////////////////////////////////////////////////////////////////

    public static void addMassToSpringBody(Mass mass, SpringBody sb, boolean updateSceneTreeSelection) {
        sb.addMass(mass);
        if (updateSceneTreeSelection)
            DataModelsFactory.getSceneTreeModel().fireSpringBodyChanged(sb);
    }

    public static void removeMassFromSpringBody(Mass mass, Scene scene, boolean updateSceneTreeSelection) {

        // Remove mass from torques
        ArrayList<Torque> torqueList = scene.getJETContext().getTorqueListThatUsesMass(mass);
        ArrayList<Torque> t2rList = new ArrayList<Torque>();

        if (torqueList != null)
            for (Torque t : torqueList)
                if (t.getMass().equals(mass))
                    t2rList.add(t);
                else
                    t.getMassList().remove(mass);

        for(Torque t : t2rList)
            StaticActions.removeTorque(t, scene);

        // Remove engine springs that uses mass
        SA_Spring.removeSpringsFromEngine(SceneHelper.getSpringListOfMass(scene, mass, true, false, false), scene);

        // Remove spring body springs that uses mass
        SA_Spring.removeSpringsFromSpringBody(SceneHelper.getSpringListOfMass(scene, mass, false, true, false), scene, false);

        // Remove scene item springs that uses mass
        SA_Spring.removeSpringsFromSceneItem(SceneHelper.getSpringListOfMass(scene, mass, false, false, true), scene, false);

        // Remove engine rotary springs that uses mass
//        removeRSpringsFromEngine(scene.getEngine().getRSpringListOfMass(mass), scene);
        SA_RSpring.removeRSpringsFromEngine(SceneHelper.getRSpringListOfMass(scene, mass, true, false, false), scene);

        // Remove spring body rotary springs that uses mass
//        removeRSpringsFromSpringBody(scene.getEngine().getRSpringListOfMass(mass), scene, false);
        SA_RSpring.removeRSpringsFromSpringBody(SceneHelper.getRSpringListOfMass(scene, mass, false, true, false), scene, false);

        // Remove walls that uses mass
//        removeWallsFromSpringBody(scene.getEngine().getWallListOfMass(mass), scene, false);
        SA_Wall.removeWallsFromSpringBody(SceneHelper.getWallListOfMass(scene, mass), scene, false);

        // Remove mass from springBodies
//        SpringBody sb = scene.getEngine().getSpringBodyThatOwnMass(mass);
        SpringBody sb = SceneHelper.getSpringBodyThatOwnMass(scene, mass);

        if (sb != null) {
            SA_Selection.removeMassFromSelection(mass, scene, true);
            sb.removeMass(mass);
            if (updateSceneTreeSelection)
                DataModelsFactory.getSceneTreeModel().fireSpringBodyChanged(sb);
        }
    }

    //public static void addMassListToSpringBody(ArrayList<Mass> massList, SpringBody sb, boolean updateSceneTreeSelection) {
    public static void addMassListToSpringBody(Collection<Mass> massList, SpringBody sb, boolean updateSceneTreeSelection) {
        sb.addMasses(massList);
        if (updateSceneTreeSelection)
            DataModelsFactory.getSceneTreeModel().fireSpringBodyChanged(sb);
    }
}
